from django.views import View

from User.models import User
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class InfoView(LoginRequiredUser, View):
    """ 用户信息 """

    def get(self, request):
        """ 查询 """
        user = request.user.user
        return Res.Json(Res.OK, result=user.Info())

    @Req.getJson
    def post(self, request):
        """ 修改 """
        nickname = request.json_dict.get("nickname")
        sex = request.json_dict.get("sex")
        birthday = request.json_dict.get("birthday")
        tall = request.json_dict.get("tall")
        weight = request.json_dict.get("weight")

        user: User = request.user.user

        if nickname:
            user.nickname = nickname
        if sex:
            user.sex = sex
        if birthday:
            user.birthday = birthday
        if tall:
            user.tall = tall
        if weight:
            user.weight = weight

        user.save()
        return Res.Json(Res.OK)
